package com.ucode.hrm.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ucode.hrm.mapper.HrmAttendanceRecordMapper;
import com.ucode.model.hrm.HrmAttendanceConfig;
import com.ucode.model.hrm.HrmAttendanceRecord;
import com.ucode.model.hrm.enums.HrmClockType;
import com.ucode.model.hrm.enums.HrmSysResultCodeEnum;
import com.ucode.springboot.starter.mybatis.core.base.BaseService;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:  考勤打卡Service
 * @since: 1.0
 * @param
 * @return:
 */
public interface HrmAttendanceRecordService  extends BaseService<Long, HrmAttendanceRecord, HrmAttendanceRecordMapper> {

    /**
     /**
     * 处理上下班打卡业务
     *
     * @param config 考勤配置
     * @param employeeId 员工ID
     * @param companyId 员工客户公司ID
     * @param projectId 员工客户公司项目ID
     * @param workDate 打卡日期
     * @param clockType 打卡类型：CLOCK_IN(上班)/CLOCK_OUT(下班)
     * @param lat 打卡纬度
     * @param lng 打卡经度
     * @param location 打卡位置描述
     * @param pics 打卡图片
     * @param remark 打卡备注
     */
    public Long processAttendance(HrmAttendanceConfig config, Long employeeId,Long companyId,Long projectId,LocalDate workDate, HrmClockType clockType, BigDecimal lat, BigDecimal lng, String location,List<String> pics, String remark);

    /**
     * 补 卡
     * @param config 考勤配置
     * @param employeeId 员工ID
     * @param companyId 员工客户公司ID
     * @param projectId 员工客户公司项目ID
     * @param clockType 补卡类型：CLOCK_IN(上班)/CLOCK_OUT(下班)
     * @param workDate 补卡日期
     * @return
     */
    public Long reissueAttendance(HrmAttendanceConfig config, Long employeeId,Long companyId,Long projectId, HrmClockType clockType, LocalDate workDate);

    /**
     * 获取员工某天打卡记录
     * @param employeeId
     * @param workDate
     * @return
     */
    public default HrmAttendanceRecord getEmployeeAttendanceRecord(Long employeeId, LocalDate workDate){
        if(ObjUtil.isNull(employeeId) || ObjUtil.isNull(workDate))
            throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED);

        Map<String, Object> condition = new HashMap<>();
        condition.put("employeeId",employeeId);
        condition.put("workDate",workDate);

        List<HrmAttendanceRecord> records = this.queryObjectForList(condition,1);
        return CollUtil.isNotEmpty(records) ? records.get(0): null;
     }

    /**
     * 校验
     * @param id
     * @return
     */
    public default HrmAttendanceRecord validate(Long id){
        if(ObjectUtil.isNull(id) )
            throw new UcodeServiceException(HrmSysResultCodeEnum.ATTENDANCERECORD_NOT_EXITS);
        //验证数据是否存在
        HrmAttendanceRecord bean = this.selectByPrimaryKey(id);
        if(ObjectUtil.isNull(bean) || bean.getDeltag()){
            throw new UcodeServiceException(HrmSysResultCodeEnum.ATTENDANCERECORD_NOT_EXITS);
        }
        return bean;
    }
}
